<?php

class PollController extends ApplicationController {
	protected $AppName = "poll";
	protected $IncludeFiles = array("poll.class.php", "poll.item.class.php");
	public $UseApps = array(array("name" => "mysql"), array("name" => "smarty"), array("name" => "account"));
	protected $HeadTags = array("<link rel=\"stylesheet\" type=\"text/css\" href=\"{\$env.url}lib/poll/views/poll.css\" />");

	function init(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");

		$HTTP = new HTTPRequest();

		$URLParameter = $HTTP->get($URLPosition);

		switch($URLParameter){
			case "show":
				$PollID = $HTTP->get($URLPosition+1);

				if(!is_numeric($PollID)){
					return $this->Language->get("poll-id-not-numeric");
				}

				if(isset($_COOKIE["poll_id-".$PollID])){
					$Result = $this->showResults($PollID, true);

					if($Result === true){
						return true;
					}
					$this->ViewFile = "message.tpl";
					return $this->Language->get("you-have-already-voted");
				}

				$ShowPoll = $this->showPoll($PollID);

				if($ShowPoll === true){
					$SmartyC = $this->UseApps["smarty"];
					$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/poll/poll.js\"></script>");

					$this->ViewFile = "show_poll.tpl";
					return true;
				}
				elseif ($ShowPoll === false) {
					return $this->Language->get("poll-error-showing");
				}
				else {
					return $ShowPoll;
				}
			break;

			case "results":
				$PollItemID = $HTTP->get($URLPosition+1);

				if(!is_numeric($PollItemID)){
					$this->ViewFile = "messages.tpl";
					return $this->Language->get("poll-id-not-numeric");
				}

				$Result = $this->showResults($PollItemID);

				if($Result === true){
					return true;
				}

				return "false";
			break;

			case "vote":
				$AnswerID = $HTTP->get($URLPosition+1);

				if(!is_numeric($AnswerID)){
					return $this->Language->get("answer-id-not-numeric");
				}

				$PollItem = new PollItem($AnswerID, $this);

				if(isset($_COOKIE["poll_id-".$PollItem->getPollID()])){
					$this->ViewFile = "message.tpl";
					return $this->Language->get("you-have-already-voted");
				}

				$PollItem->setVotes($PollItem->getVotes()+1);

				if($PollItem->update() == true){
					setcookie("poll_id-".$PollItem->getPollID(), "true", time()+60*60*24*365, "/");
					return $this->Language->get("thank-you-for-voting");
				}

				$this->ViewFile = "message.tpl";
				return $this->Language->get("voting-failed");
			break;
		}
	}

	function showResults($id, $is_poll_id = false, $admin = false){
		if(!is_numeric($id)){
			return $this->Language->get("poll-id-not-numeric");
		}

		if($is_poll_id == false){
			$PollItem = new PollItem($id, $this);

			$PollID = $PollItem->getPollID();
		}
		else {
			$PollID = $id;
		}

		$Poll = new Poll($PollID, $this);

		if($Poll->canShowResults() == false && $admin == false){
			return false;
		}

		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();

		$Smarty->assign("Poll", $Poll);

		$this->ViewFile = "show_results.tpl";
		return true;
	}

	function showPoll($poll_id){
		if(!is_numeric($poll_id)){
			return false;
		}

		$Poll = new Poll($poll_id, $this);

		if($Poll->getID() == null){
			return false;
		}

		if($Poll->isActive() == false){
			return $this->Language->get("poll-not-active");
		}

		$AccountC = $this->UseApps["account"];

		if(!($AccountC->isLogged()) && $Poll->membersOnly()){
			return $this->Language->get("poll-members-only");
		}

		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();

		$Smarty->assign("Poll", $Poll);

		return true;
	}

	function templateShowPoll($params = array()){
		$PollID = $params[0];
		$AdminPanel = ($params[1] == "true") ? true : false;

		if($PollID == "latest"){
			$MySQL = $this->UseApps["mysql"];

			$LatestPoll = $MySQL->query("SELECT id FROM {$MySQL->getPrefix()}polls ORDER BY id DESC LIMIT 1");
			$Fetch = mysql_fetch_object($LatestPoll);

			$PollID = $Fetch->id;
		}

		if(!is_numeric($PollID)){
			return $this->Language->get("poll-id-not-numeric");
		}

		if(isset($_COOKIE["poll_id-".$PollID]) || $AdminPanel == true){
			$Result = $this->showResults($PollID, true, $AdminPanel);

			if($Result === true){
				return true;
			}

			$this->ViewFile = "message.tpl";
			return $this->Language->get("you-have-already-voted");
		}

		$ShowPoll = $this->showPoll($PollID);

		if($ShowPoll === true){
			$SmartyC = $this->UseApps["smarty"];
			$SmartyC->addHeadTag("<script type=\"text/javascript\" src=\"{\$env.url}lib/poll/poll.js\"></script>");

			$this->ViewFile = "show_poll.tpl";
			return true;
			}
		elseif ($ShowPoll === false) {
			$this->ViewFile = "message.tpl";
			return $this->Language->get("poll-error-showing");
		}
		else {
			return $ShowPoll;
		}
	}
}

?>